﻿
$(window).load(function () {

    //lazy load
    lzLoading();

    $('.flexslider').flexslider({
        animation: "slide",
        controlNav: false
    });
    
    // dong ho dem nguoc
    //var date = $('.ProductHotPlc input.timedown').val();
    //timeClockdown(date);
    
    //hover vao ulTab
    hoverUlTabHome();
    
    //hover show quick view and order
    animateItemProduct();
    
    //Home: tab footer
    ShowTabNewsFooter();
    ShowTabTextFooter();

    // phan trang trong prouctHot
    PaggingOnProductHot();
    
});

function PaggingOnProductHot() {
    $('.hot-pag-back').click(function () {
        var page = $('.productHot-item input[name$=hidPageProductHot]').val();
        var totalPage = $('.productHot-item input[name$=hidTotalPageProductHot]').val();
        var valPage = parseInt(page) - 1;

        if (valPage <= 0) {
            valPage = 1;
        }

        $('.pro-hot-day').append('<img src="/images/icons/gif-load-small.gif" width="30" height="30" alt="loading-product hot" class="loadingProducthot" />');
        setTimeout(function () {
            $.ajax({
                type: 'POST',
                dataType: 'html',
                data: { page: valPage },
                cache: false,
                url: '/Home/ProductHotGetPaggingByAjax',
                success: function (data) {
                    $('.pro-hot-day .ProductHotPlc').html(data);
                    //set lai cuurentPage
                    $('.productHot-item input[name$=hidPageProductHot]').val(valPage);
                    $('.price-pagging').text(valPage + '/' + totalPage);
                    //--Xoa loading
                    $('.pro-hot-day .loadingProducthot').remove();
                },
                error: function () {
                    Console.log('Đã có lỗi xảy ra!');
                }
            });
        },300);
    });
    $('.hot-pag-next').click(function () {
        var page = $('.productHot-item input[name$=hidPageProductHot]').val();
        var totalPage = $('.productHot-item input[name$=hidTotalPageProductHot]').val();
        var valPage = parseInt(page) + 1;

        if (valPage >= totalPage) {
            valPage = totalPage;
        }
        $('.pro-hot-day').append('<img src="/images/icons/gif-load-small.gif" width="30" height="30" alt="loading-product hot" class="loadingProducthot" />');

        setTimeout(function () {
            $.ajax({
                type: 'POST',
                dataType: 'html',
                data: { page: valPage },
                cache: false,
                url: '/Home/ProductHotGetPaggingByAjax',
                success: function (data) {
                    $('.pro-hot-day .ProductHotPlc').html(data);
                    //set lai cuurentPage
                    $('.productHot-item input[name$=hidPageProductHot]').val(valPage);
                    $('.price-pagging').text(valPage + '/' + totalPage);
                    //--Xoa loading
                    $('.pro-hot-day .loadingProducthot').remove();
                },
                error: function () {
                    Console.log('Đã có lỗi xảy ra!');
                }
            });
        },300);
        
    });
}
//Footer bottom 1-----------------------------------

function ShowTabNewsFooter() {
    $('.tabFooter li').click(function() {
        $('.tabFooter li').removeClass('tab-f-ac');
        $(this).addClass('tab-f-ac');
        $('ul.tab-f-sub').hide();
        $(this).find('ul.tab-f-sub').show();
    });
}

function ShowTabTextFooter() {
    $('.ft-box ul li a').click(function() {
        //$('.ft-box ul li .ft-sub-box').hide();
        $('.ft-box ul li a').removeClass('ft-ac');
        $(this).next().toggle();
        $(this).addClass('ft-ac');
    });
}

// Advertise Home ----------------------------------
$('.bb_adv_box').hover(function () {
    var cmd = $(this);
    cmd.find('.bb_begai_lnk,.bb_betrai_lnk').stop().animate({
        top: '150px'
    });
}, function () {
    var cmd = $(this);
    cmd.find('.bb_begai_lnk,.bb_betrai_lnk').stop().animate({
        top: '129px'
    });
});

// Di chuot vao anh nha cung cap se hien thi lai mau
$('ul.providerList-cate li img').hover(function () {
    $(this).removeClass('desaturate');
}, function () {
    $(this).addClass('desaturate');
});


$('.bb_adv_box321').hover(function () {
    var cmd = $(this);
    cmd.find('.bb_icon_antuong').stop().animate({
        top: '67px'
    });

    cmd.find('.bb_icon_quatang').stop().animate({
        right: '40px'
    });

    cmd.find('.bb_adv_b_lnk').stop().animate({
        top: '10px'
    });
    cmd.find('.bb_quatang_poi').stop().animate({
        top: '125px'
    });
}, function () {
    var cmd = $(this);
    cmd.find('.bb_icon_antuong').stop().animate({
        top: '0px'
    }, 'slow');
    cmd.find('.bb_icon_quatang').stop().animate({
        top: '12px'
    });
    cmd.find('.bb_icon_quatang').stop().animate({
        right: '0px'
    });
    cmd.find('.bb_adv_b_lnk,.bb_quatang_poi').stop().animate({
        top: '139px'
    });
});

// NEWS Information -------------------------------
$('.bb_main_listtab ul li .bb_main_contenttab:first').show();
$('.bb_main_titleTab').click(function () {
    var cmd = $(this);
    $('.bb_main_listtab ul li .bb_main_contenttab').hide();
    $('.bb_main_listtab ul li h1').hide();
    cmd.parent().children('h1').show();
    cmd.parent().children('.bb_main_contenttab').show();
});

function timeClockdown(timeVal) {
    if (timeVal!=null & timeVal!=undefined){
        var timer = setInterval(function () {
            var seconds = remaining.getSeconds(timeVal);
            var remainingTime = remaining.getString(seconds, null, true);
            var remainingTime3 = remaining.getStringDigital(seconds);
        
            if (remainingTime == '') {
                $('.p-d-clock').html('...');
                clearInterval(timer);
            } else {
                remainingTime3 = returnString(remainingTime3);
                //alert(remainingTime3)
                $('.p-d-clock').html(remainingTime3);
            }
        }, 100);
    }
}

function returnString(str) {
    var strReturn = '';
    if (str != undefined && str != '') {
        $.each(str, function (index, value) {
            if (value != '' && value != ':') {
                strReturn += '<span>' + value + '</span>';
            } else {
                strReturn += '<i>:</i>';
            }
        });
    }
    return strReturn;
}

function hoverUlTabHome() {
    $('.ulTab li').click(function () {
        $('.ulTab li').removeClass('tab-ac');
        $(this).addClass('tab-ac');
        var idx = $(this).index();
        $('.tab-home-right .tab-content').addClass('tab-show');
        setTimeout(function () {
            $('.tab-home-right .tab-content:eq(' + idx + ')').removeClass('tab-show');
        }, 500);
    });
}

function animateItemProduct() {
    //first
    $('.pItem-1').hover(function () {
        $(this).children('.quickview').stop().animate({ left: '30%', opacity: '0.8' }, 300);
        $(this).children('.quickorder').stop().animate({ right: '23%', opacity: '0.8' }, 300);
    }, function () {
        $(this).children('.quickview').stop().animate({ left: '20%', opacity: '0' }, 300);
        $(this).children('.quickorder').stop().animate({ right: '13%', opacity: '0' }, 300);
    });

    //more
    $('.pItem-2,.pItem-3, .pItem-4 ,.pItem-5').hover(function () {
        $(this).children('.quickview').stop().animate({ left: '30%', opacity: '0.8' }, 300);
        $(this).children('.quickorder').stop().animate({ right: '29%', opacity: '0.8' }, 300);
    }, function() {
        $(this).children('.quickview').stop().animate({ left: '20%', opacity: '0' }, 300);
        $(this).children('.quickorder').stop().animate({ right: '19%', opacity: '0' }, 300);
    });
}
